<template>
  <div>
    <div class="kong"></div>
    <div class="adressInfo whiteBg">
      <p><span>收货人</span><input type="text" placeholder="收货人名称" ref="input1" v-model="inputName"/></p>
      <p><span>联系电话</span><input type="tel" placeholder="手机号" ref="input2" v-model="inputTel"></p>
      <p><span>收货地址</span>
        <textarea @keydown="keydown" v-if="!addId" maxlength="40" placeholder="请输入地址" ref="input3" v-model="inputRess"
                  contenteditable="true"></textarea>
        <textarea @keydown="keydown" v-if="addId" maxlength="40" placeholder="请输入地址" ref="input3" v-model="inputRess"
                  contenteditable="true"></textarea>
        <!--<textarea @keydown="keydown" v-if="!addId" maxlength="40" placeholder="请输入地址" name="" rows="" cols="" ref="input3" v-model="inputRess"></textarea>-->
        <!--<textarea @keydown="keydown" v-if="addId" maxlength="40" placeholder="请输入地址" name="" rows="" cols="" ref="input3" v-model="inputRess"></textarea>-->
      </p>
    </div>
    <div class="addFriendBtn1" @click="addmess">保存</div>

  </div>
</template>
<script>
  import * as types from '../../vuex/types.js'
  import {mapState} from 'vuex'
  import * as types1 from '../../vuex/appendType'
  import listLoading from '../../componentes/global/listLoading.vue'
  import getDate from '../../service/getData'
  import {Toast} from 'mint-ui';
  import {Storage} from 'src/utils/storage.js'

  export default {
    components: {
      listLoading
    },
    data() {
      return {
        inputTel: '',
        inputRess: '',
        inputName: '',
        addId: '',//判断是否第一次
      }
    },
    created: function () {
      this.addId = this.$route.query.addId
      if (this.$route.query.addId) {
        //输入框有之前值
        this.inputTel = Storage.get('changeRess').inputTel
        this.inputRess = Storage.get('changeRess').inputRess
        this.inputName = Storage.get('changeRess').inputName
      }
    },
    computed: {
      ...mapState({
        userInfo: state => state.user.userInfo,
      })
    },
    methods: {
      addmess: function () {

        var me = this
        if (me.inputTel && me.inputRess && me.inputName && (/^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/.test(me.inputTel))) {
          if (!me.$route.query.addId) {  //第一次添加地址
            me.$store.commit('changeState', true)
            getDate.addRess({
              method: 'post',
              data: {
                usrAddress: this.$refs.input3.value,
                usrId: me.userInfo.usrId,
                usrName: this.$refs.input1.value,
                usrTel: this.$refs.input2.value
              }
            }).then(function (rep) {
              if (rep.respCode == '00') {
                me.$router.push({'name': 'firmOrder'})
              }
            })
          } else if (me.$route.query.addId) {  //改变地址
            me.$store.commit('changeState', true)
            getDate.changeRess({
              method: 'post', data: {
                addId: Storage.get('changeRess').addId,
                usrAddress: this.$refs.input3.value,
                usrId: this.userInfo.usrId,
                usrName: this.$refs.input1.value,
                usrTel: this.$refs.input2.value
              }
            }).then(function (rep) {
              if (rep.respCode == '00') {
                Toast({
                  message: "更换成功",
                  duration: 800
                })
                me.$router.push({'name': 'firmOrder'})
              }
            })
          }
        } else if (!me.inputName) {
          Toast({
            message: "请输入收货人",
            duration: 800
          })
        } else if (!me.inputTel) {
          Toast({
            message: "请输入联系电话",
            duration: 800
          })
        } else if (!me.inputRess) {
          Toast({
            message: "请输入收货地址",
            duration: 800
          })
        } else if (!(/^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/.test(me.inputTel))) {
          Toast({
            message: "请输入正确的电话号码",
            duration: 800
          })
        }
      },
      keydown() {
        var event = window.event || arguments.callee.caller.arguments[0];
        var x = event.clientX;
        var y = event.clientY;
        console.log(x + "+" + y)
        if (this.inputRess.length == 40) {
          Toast({
            message: "收货地址不能超过40字",
            duration: 800
          })
        }
      }
    }
  }
</script>
<style scoped>
  .kong {
    height: .66rem;
  }

  .adressInfo p {
    height: 3.17rem;
    line-height: 3.17rem;
    padding: 0 1.5rem 0 1.5rem;
  }

  .adressInfo p:nth-child(2) {
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
  }

  .adressInfo p:nth-child(3) span {
    line-height: 4.8rem;
  }

  .adressInfo p span {
    position: absolute;
    left: 1.5rem;
    color: #333;
  }

  .adressInfo p input {
    float: right;
    text-align: right;
    height: 3rem;
    color: #666;
    font-size: .9rem;
  }

  .adressInfo p:nth-of-type(3) span {
    z-index: 2;
  }

  textarea {
    float: right;
    text-align: right;
    font-size: .9rem;
    width: 16rem;
    resize: none;
    outline: none;
    line-height: 1.5rem;
    padding-top: 1.8rem;
    color: #666;
  }

  .addFriendBtn1 {
    width: 75%;
    height: 2.4rem;
    line-height: 2.4rem;
    background: #ec4e8a;
    color: #fff;
    margin: 6rem auto 0;
    text-align: center;
    border-radius: .2rem;
  }

  textarea::-webkit-input-placeholder {
    text-align: right;
  }

  textarea:-moz-placeholder {
    text-align: right;
  }

  textarea::-moz-placeholder {
    text-align: right;
  }

  textarea:-ms-input-placeholder {
    text-align: right;
  }
</style>
